package com.wuhuahe.learning.lagou.service.user.service.impl;

import com.wuhuahe.learning.lagou.service.dubbo.api.UserService;
import com.wuhuahe.learning.lagou.service.dubbo.api.protol.RegisterInfo;
import com.wuhuahe.learning.lagou.service.dubbo.api.protol.UserInfo;
import com.wuhuahe.learning.lagou.service.user.mapper.UserMapper;

import com.wuhuahe.learning.lagou.service.user.pojo.User;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean register(RegisterInfo registerInfo) {
        User user = new User();
        user.setEmail(registerInfo.getEmail());
        user.setPassword(registerInfo.getPassword());
        Timestamp timestamp = new Timestamp(new Date().getTime());
        user.setCreated_date(timestamp);
        user.setUpdated_date(timestamp);
        try {
            int userId = userMapper.insertUser(user);
            return true;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean isRegistered(String email) {

        User user = userMapper.findByEmail(email);
        return user != null;
    }

    @Override
    public UserInfo getUserByEmailAndPassword(String email, String password) {
        User user = userMapper.findByEmailAndPassword(email, password);
        if(user == null) return null;
        UserInfo userInfo = new UserInfo();
        userInfo.setId(user.getId());
        userInfo.setEmail(user.getEmail());
        userInfo.setPassword(user.getPassword());
        userInfo.setCreated_date(user.getCreated_date());
        userInfo.setUpdated_date(user.getUpdated_date());
        return userInfo;
    }
}
